from __future__ import print_function
import string


def hexdump(stream, return_hex=False):
    printable_chars = string.digits + string.letters + string.punctuation + " "
    hex = ""
    printable_str = ""
    for ch in stream:
        code = ord(ch)
        if return_hex:
            hex += "{0:02X} ".format(code)
        if ch in printable_chars:
            printable_str += ch
        else:
            printable_str += "."

    if return_hex:
        return hex, printable_str
    else:
        return printable_str

def get_int(msg, minimum=None, default=None):
    while True:
        try:
            line = input(msg)
            if not line and default is not None:
                return default
            i = int(line)
            if (minimum is not None) and (i < minimum):
                print("must be >=", minimum)
            else:
                return i
        except ValueError as err:
            print(err)


def get_value(msg, acceptable=None, default=None):
    prompt_suffix = ""
    if acceptable:
        prompt_suffix += "(" + "|".join(acceptable) + ")"
    if default:
        prompt_suffix += " [" + default + "]"

    if len(prompt_suffix):
        msg += " " + prompt_suffix + "> "

    while True:
        try:
            response = raw_input(msg)
            if not response and default is not None:
                response = default
            if acceptable and response in acceptable:
                return response 
        except ValueError as err:
            print(err)

